import { getApisData } from '@/services/cpasRequest';

interface Api {
  Disclosure: object;
}
const ROOT = '/cpasApi';
export async function getNbwltjData(params: { dbname: string; isQM: boolean; data_id: string }) {
  if (!params.dbname) return [];
  const res = postFetchData('account.nbwltj', params);
  return res;
}

export async function getZLList(params: { dbname: string; isQM: boolean }) {
  if (!params.dbname) return [];
  const res = postFetchData('account.nbwltj_col', params);
  console.log(res);
  return res;
}

// 删除表，检擦吴删除
export async function deleteTable(params: {
  dbname: string;
  tableName: string;
  data_id?: string;
  from_type?: string;
}) {
  if (!params.dbname) return [];
  const res = await postFetchData('delete.table.truncate', params);
  return res;
}

// 更新 BB_ReportIntercourse
export async function updateBB_ReportInter(dbname: string, paramsData: any[]) {
  const postData = {
    apis: [
      {
        name: 'sqlApi.update',
        params: { name: `[${dbname}]..BB_ReportIntercourse`, rows: paramsData },
      },
    ],
    reload: true,
    params: {},
  };
  const resApis = await getApisData(postData);
  const resUpdate = resApis.data['sqlApi.update'];
  if (resApis.ok && resUpdate.ok) {
    return Promise.resolve({
      data: 'BB_ReportIntercourse, 更改信息成功！',
      success: true,
    });
  }
  return Promise.resolve({
    data: resApis.data,
    success: false,
  });
}

// 报表注释与分析 - 披露表 - methodAPI - 固定资产 接口
/**
 *
 * @param name
 * @param dbName
 * @param data_id
 * @returns
 */
export async function getDisclosureApiData(
  name: string,
  dbName: string,
  data_id: string,
): Promise<API.SerResult> {
  const pd: API.Disclosure = {
    name: name, // 接口名
    dbName: dbName, //数据库名
    data_id: data_id, // 年度
  };

  try {
    const res = fetch(`${ROOT}/disclosure/getDisclosureApiData`, {
      method: 'POST',
      headers: {
        'Content-Type': 'application/json',
      },
      body: JSON.stringify(pd),
    });

    return res.then((response) => response.json());
  } catch (error) {
    return Promise.resolve(false);
  }
}

// Example POST method implementation:
export async function postFetchData(name: string, params: any) {
  const pd: API.Disclosure = {
    name: name, // 接口名
    params: params,
  };
  // Default options are marked with *
  return fetch(`${ROOT}/sqlapis/getApiData?api=${name}`, {
    method: 'POST', // *GET, POST, PUT, DELETE, etc.
    mode: 'cors', // no-cors, *cors, same-origin
    cache: 'no-cache', // *default, no-cache, reload, force-cache, only-if-cached
    credentials: 'same-origin', // include, *same-origin, omit
    headers: {
      'Content-Type': 'application/json',
      // 'Content-Type': 'application/x-www-form-urlencoded',
    },
    redirect: 'follow', // manual, *follow, error
    referrerPolicy: 'no-referrer', // no-referrer, *no-referrer-when-downgrade, origin, origin-when-cross-origin, same-origin, strict-origin, strict-origin-when-cross-origin, unsafe-url
    body: JSON.stringify(pd), // body data type must match "Content-Type" header
  }).then((response) => response.json());
  // return response; // parses JSON response into native JavaScript objects
}
